package com.elong.android.specialhouse.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import com.elong.android.specialhouse.IConfig;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.ui.CustomDialogBuilder;
import com.elong.android.specialhouse.ui.CustomDialogBuilder.Observer;
import com.elong.android.specialhouse.utils.ElongValidator;
import com.elong.android.specialhouse.utils.Utils;

public abstract class BaseFragementActivity extends FragmentActivity implements OnClickListener,Observer {
	
	public final static String TAG = "BaseFragementActivity";
	
	// 传递的数据信息
	protected Bundle myBundle;
	
//	private static PreferencesHelper s_preferencesHelper;
//
//	// 只用于保存帐户和密码
//	private SharedPreferences s_prefrences;
//
	/**
	 * 状态标识： 正在结束Activity
	 */
	protected boolean m_isFinishing;
	
	/**
	 * 状态标识：刷新是否结束
	 */
//	protected boolean m_refreshFinished;
	
	/**
	 * 刷新时是否压缩网络数据
	 */
//	protected boolean m_refreshCompressData;
	
	
	// Activity 自有成员：loading框和超时提示框
	private CustomDialogBuilder loadingDialog;
	private CustomDialogBuilder timeoutDialog;
	
//	public PreferencesHelper getPreferencesHelper() {
//		if (s_preferencesHelper == null) {
//			s_preferencesHelper = new PreferencesHelper(getApplicationContext());
//			InputStream is = null;
//			try {
//				is = getResources().openRawResource(R.raw.preferences);
//				s_preferencesHelper.inflate(is);
//				is.close();
//			} catch (Exception e) {
//				LogWriter.logException(TAG, LogWriter.LOG_LEVEL_NORMAL, e);
//			}
//			is = null;
//		}
//		return s_preferencesHelper;
//	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 初始化本地数据
		initLocalData(savedInstanceState);
		// 设置布局文件
		initContentView();
		// 初始化View控件的值,状态和事件监听等
		initViewByLocalData();
	}

	/**
	 * 
	 * @Title: initViews
	 * @Description: 初始化页面布局设置
	 * @param
	 * @return void
	 * @throws
	 */
	private void initViewByLocalData() {
		// button back
		View back = findViewById(R.id.common_head_back);
		if (back != null) {
			back.setOnClickListener(this);
		}

	}

	protected abstract void initContentView();

	private void initLocalData(Bundle savedInstanceState) {
		myBundle = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;	
		
		if (myBundle == null) {
			myBundle = new Bundle();
		}
		
//		if (s_preferencesHelper == null || s_prefrences == null) {
//			s_preferencesHelper = getPreferencesHelper();
//			s_prefrences = getPrefrences();
//		}
		
		m_isFinishing = false;
//		m_refreshFinished = false;
//		m_refreshCompressData = IConfig.COMPRESS_NETWORK_DATA;

		if (IConfig.MANAGE_ACTIVITYSTACK_MANUALLY) {
			Utils.pushActivity(this);
		}

		m_isFinishing = false;
		ElongValidator.setSpecialCharacters(this);

		loadingDialog = new CustomDialogBuilder(this, CustomDialogBuilder.LOADING_DIALOG_IDS, 0);
		loadingDialog.setCancelable(false);
		loadingDialog.setObserver(this);
		timeoutDialog = new CustomDialogBuilder(this, Utils.DIALOG_IDS, 1);
		timeoutDialog.setMessage(R.string.network_timeout_prompt);
		timeoutDialog.setCancelable(false);
		timeoutDialog.setObserver(this);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}
	
//	public SharedPreferences getPrefrences() {
//		if (s_prefrences == null) {
//			s_prefrences = PreferenceManager.getDefaultSharedPreferences(this);
//		}
//		return s_prefrences;
//	}
	
	public boolean isAlive() {
		return !m_isFinishing;
	}
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.common_head_back:
			back();
			break;
		}
	}

	private void back() {
		finish();
		// 添加界面切换效果，注意只有Android的2.0(SdkVersion版本号为5)以后的版本才支持
		int version = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
		if (version > 4) {
			overridePendingTransition(0, R.anim.push_right_out);
		}

	}

	/* 打开新的Activity */
	public void startActivity(Intent intent) {
		long currentTimeMillis = System.currentTimeMillis();
		intent.putExtra("mcurrentTimeMillis", currentTimeMillis);
		super.startActivity(intent);
		int version = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
		if (version > 4) {
			overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		}
	}
	
	/**
	 * 重写按键处理方法 这里主要实现Back键被按下后的流程（屏蔽系统默认的Back流程，添加数据清理等额外操作）
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {

//		Debug.u(TAG, "BaseActivity onKeyDown()");
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			back();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
